package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.entity.*;
import com.itms.entity.Package;
import com.itms.enums.TableName;
import com.itms.mapper.*;
import com.itms.pojo.Part;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
public class PartJob {

    @Autowired
    private LogService logService;

    @Reference
    private BasicDataService basicDataService;

    @Autowired
    private SycService sycService;

    @Autowired
    private PartMapper partMapper;

    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private LocMapper locMapper;

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private PartProjectMapper partProjectMapper;

    @Scheduled(initialDelayString = "${PartJob.initialDelayString}",fixedDelayString="${PartJob.fixedDelayString}" )
    public void doSchedule() {
        List<ErrorMsg> errorList = new ArrayList<>();
        try {
            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBPart.toString());
            Date itms = this.sycService.getMaxSyncDate(TableName.MDBPart.toString());
            if (mdb==null) {
                return;
            }
            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) {
                return;
            }
                List<Part> list = this.basicDataService.getPartList(itms);
                for (Part c : list) {
                    if(!"SGM".equals(c.getCustomerCode())){
                        continue;
                    }
                    Supplier supplier = null;
                    Project project = null;
                    Package pk = null;
                    Loc dlvLoc = null;
                    Loc picLOC = null;
                    Loc lcmLOC = null;
                    Loc dlcmLOC = null;
                    if (c.getSupplierCode() != null) {
                        supplier = this.supplierMapper.getSupplierBySupplierCode(c.getSupplierCode(),"1");
                        if (supplier == null) {
                            errorList.add(new ErrorMsg(c, c.getSupplierCode() + "供应商不存在 "));
                            this.logService.saveDetailLog(new ErrorMsg(c, c.getSupplierCode() + "供应商不存在 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                            continue;
                        }
                    }else {
                        errorList.add(new ErrorMsg(c,  "供应商不能为空 "));
                        this.logService.saveDetailLog(new ErrorMsg(c,  "供应商不能为空 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                        continue;
                    }
                    if (c.getProjectCode() != null){
                        project = this.projectMapper.getProjectByProjectCode(c.getProjectCode(),"1");
                        if (project == null) {
                            errorList.add(new ErrorMsg(c, c.getProjectCode() + "项目代码不存在 "));
                            this.logService.saveDetailLog(new ErrorMsg(c, c.getProjectCode() + "项目代码不存在 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                            continue;
                        }
                    }else {
                        errorList.add(new ErrorMsg(c,  "项目代码不能为空 "));
                        this.logService.saveDetailLog(new ErrorMsg(c,  "项目代码不能为空 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                        continue;
                    }
                    if (c.getPackageCode() != null) {
                        pk = this.packageMapper.getPacakgeByPacakgeCode(c.getPackageCode(),"1");
                        if (pk == null) {
                            errorList.add(new ErrorMsg(c, c.getPackageCode() + "包装箱号不存在 "));
                            this.logService.saveDetailLog(new ErrorMsg(c, c.getPackageCode() + "包装箱号不存在 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                            continue;
                        }
                    }else {
                        errorList.add(new ErrorMsg(c,  "包装箱号不能为空 "));
                        this.logService.saveDetailLog(new ErrorMsg(c,  "包装箱号不能为空 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                        continue;
                    }
                    if (c.getDlvLoc() != null) {
                        dlvLoc = this.locMapper.selectByLocCode(c.getDlvLoc(),"1");
                        if (dlvLoc == null) {
                            this.logService.saveDetailLog(new ErrorMsg(c, c.getDlvLoc() + "站点不存在 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                            errorList.add(new ErrorMsg(c, c.getDlvLoc() + "站点不存在 "));
                            continue;
                        }
                    }else {
                        errorList.add(new ErrorMsg(c,  "站点不能为空 "));
                        this.logService.saveDetailLog(new ErrorMsg(c,  "站点不能为空 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                        continue;
                    }
                    if (c.getPicLOC() != null) {
                        picLOC = this.locMapper.selectByLocCode(c.getPicLOC(),"1");
                        if (picLOC == null) {
                            this.logService.saveDetailLog(new ErrorMsg(c, c.getPicLOC() + "站点不存在 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                            errorList.add(new ErrorMsg(c, c.getPicLOC() + "站点不存在 "));
                            continue;
                        }
                    }else {
                        errorList.add(new ErrorMsg(c,  "站点不能为空 "));
                        this.logService.saveDetailLog(new ErrorMsg(c,  "站点不能为空 "),TableName.MDBPart.toString(),this.getClass().getName(),c.getPartReference());
                        continue;
                    }
                    com.itms.entity.Part part = this.partMapper.selectByCode(c.getPartReference(),null,dlvLoc.getId(),picLOC.getId(),supplier.getId());

                    if (part == null) {
                            part = new com.itms.entity.Part();
                            setPart(c, supplier, pk, dlvLoc, picLOC, part);
                            part.setCreateDate(new Date());
                            part.setCreateUserId(new BigDecimal(9999));
                            this.partMapper.insertSelective(part);
                        } else {
                            setPart(c, supplier, pk, dlvLoc, picLOC, part);
                            this.partMapper.updateByPrimaryKeySelective(part);
                        }
                    if (c.getProjectCode() != null){
                        PartProject partProject = this.partProjectMapper.selectByProjectIdPartId(project.getId(),part.getId());
                        if (partProject == null){
                            partProject = new PartProject();
                            partProject.setPartId(part.getId());
                            partProject.setProjectId(project.getId());
                            partProject.setCreateDate(new Date());
                            partProject.setCreateUserId(new BigDecimal(9999));
                            partProject.setLastUpdateDate(new Date());
                            this.partProjectMapper.insertSelective(partProject);
                        }
                    }
                }
                if (errorList.size() == 0)
                    this.sycService.save(mdb,TableName.MDBPart.toString());
                this.logService.saveErrorList(errorList, TableName.MDBPart.toString(), this.getClass().getName());
        } catch (Exception e) {
            e.printStackTrace();
            this.logService.saveLog(e, TableName.MDBPart.toString(), this.getClass().getName());
        }
    }

    private static void setPart(Part c, Supplier supplier, Package pk, Loc dlvLoc, Loc picLOC, com.itms.entity.Part part) {
        part.setPartReference(c.getPartReference());
        part.setPartName(c.getPartName());
        part.setPartType("");
        if (c.getPackageNumber() != null)
            part.setPiecespeRouterPack(Long.parseLong(c.getPackageNumber()));
        part.setLength(new BigDecimal(c.getLength().longValue()/10));
        part.setWidth(new BigDecimal(c.getWidth().longValue()/10));
        part.setHeight(new BigDecimal(c.getHeight().longValue()/10));
        part.setIsValid(c.getIsValid());
        part.setWeight(c.getWeight());
        part.setLayersQty(c.getLayersQty());
        if (c.getPackageCode() != null && pk != null)
            part.setPackageId(pk.getId());
        if (c.getSupplierCode() != null && supplier != null)
            part.setSupplierId(supplier.getId());
        /**
         卸货点 DLVLOC
         提货点 PICLOC
         */
        if (c.getPicLOC() != null)
            part.setPickupLocationId(picLOC.getId());
        if (c.getDlvLoc() != null)
            part.setDeliveryLocationId(dlvLoc.getId());
        part.setLastUpdateUserId(new BigDecimal(9999));
        part.setLastUpdateDate(new Date());
    }
}
